package org.hot100.h053;

/**
 * @Author yu
 * @Date 2022/8/3
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {-2, 1, -3, 4, -1, 2, 1, -5};

        Solution solution = new Solution();
        System.out.println(solution.maxSubArray2(ints));

    }

    public int maxSubArray(int[] nums) {
        int pre = 0;
        int sum = nums[0];
        for (int num : nums) {

            // 记录到num为止子字符串最大子数组和
            pre = Math.max(pre + num, num);

            // 记录最大数
            sum = Math.max(pre, sum);
        }
        return sum;
    }

    public int maxSubArray2(int[] nums) {
        int pre = 0, sum = nums[0];
        for (int num : nums) {
            pre = Math.max(pre + num, num);
            sum = Math.max(sum, pre);
        }
        return pre;
    }

}
